package com.kun.base.web.controller;

import com.kun.base.config.Config;
import com.kun.base.fs.FileSystem;
import com.kun.base.fs.FileSystemFactory;
import com.kun.base.web.service.BaseHelp;
import com.xiaoleilu.hutool.io.IoUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletResponse;
import java.io.*;

/**
 * @author likun
 * @date 2016/12/30
 */
@Controller
@RequestMapping("/public")
public class PublicController implements BaseHelp {

    private FileSystem fileSystem = FileSystemFactory.getFileSystem();

    @RequestMapping("/getHeadImage")
    public void getHeadImage(String path, HttpServletResponse response) {
        response.setHeader("ragma", "No-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", 0);
        //获取文件后缀
        String str = path.substring(path.lastIndexOf(".") + 1, path.length());
        response.setContentType("image/" + str);
        String filepath = Config.HEAD_IMAGE_PATH + path;
        if (fileSystem.exists(filepath)) {
            try (
                    InputStream inputStream = fileSystem.read(filepath);
                    OutputStream os = response.getOutputStream();
            ) {
                IoUtil.copy(inputStream, os);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
